package com.example.wechat;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;//没有，导进去即可
import androidx.fragment.app.FragmentManager;


import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;



public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Fragment fragment1,fragment2,fragment3,fragment4;
    FragmentManager  fm;



    LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        fragment1=new Fragment1();
        fragment2=new Fragment2();
        fragment3=new Fragment3();
        fragment4=new Fragment4();


        linearLayout1=findViewById(R.id.LinearLayout1);
        linearLayout2=findViewById(R.id.LinearLayout2);
        linearLayout3=findViewById(R.id.LinearLayout3);
        linearLayout4=findViewById(R.id.LinearLayout4);
        fm=getSupportFragmentManager();

        initial();//4个fragment放一起
        fragmenthide();//藏起来
        fragmentshow(fragment1);//默认的聊天界面

        //点击show出来 switch case

        linearLayout1.setOnClickListener(this);//监听对屏幕的
        linearLayout2.setOnClickListener(this);//监听对屏幕的
        linearLayout3.setOnClickListener(this);//监听对屏幕的
        linearLayout4.setOnClickListener(this);//监听对屏幕的

    }

    private void fragmenthide() {//藏起来
        FragmentTransaction ft=fm.beginTransaction()
                .hide(fragment1)
                .hide(fragment2)
                .hide(fragment3)
                .hide(fragment4);
        ft.commit();
    }

    private void initial() {//压入content
        FragmentTransaction ft=fm.beginTransaction()//事物 管理交互动作
                .add(R.id.content,fragment1)
                .add(R.id.content,fragment2)
                .add(R.id.content,fragment3)
                .add(R.id.content,fragment4);
        ft.commit();

    }

    @Override
    public void onClick(View view) {
        fragmenthide();
        if (view.getId()==R.id.LinearLayout1){
            fragmentshow(fragment1);
        }else if (view.getId()==R.id.LinearLayout2){
            fragmentshow(fragment2);
        }else if (view.getId()==R.id.LinearLayout3){
            fragmentshow(fragment3);
        }else if(view.getId()==R.id.LinearLayout4){
            fragmentshow(fragment4);
        }


//        switch(view.getId()){
//            case R.id.LinearLayout1:fragmentshow(fragment1);break;
//            case R.id.LinearLayout2:fragmentshow(fragment2);break;
//            case R.id.LinearLayout3:fragmentshow(fragment3);break;
//            case R.id.LinearLayout4:fragmentshow(fragment4);break;
//
//        }
    }

    private void fragmentshow(Fragment fragment) {
        FragmentTransaction ft=fm.beginTransaction()
                .show(fragment);
        ft.commit();
    }
    //点谁谁出来


}